package com.zhou.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zhou.constant.MessageConstant;
import com.zhou.entity.PageResult;
import com.zhou.entity.QueryPageBean;
import com.zhou.entity.Result;
import com.zhou.pojo.User;
import com.zhou.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;

    /**
     * 获取当前登录的用户名
     *
     * @return
     */
    @RequestMapping("/getUsername")
    public Result getUsername() {
        try {
            org.springframework.security.core.userdetails.User user =
                    (org.springframework.security.core.userdetails.User)
                            SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) {
        return userService.findByPage(queryPageBean);
    }

    /**
     * 新增用户
     *
     * @param user
     * @param roleIds
     * @return
     */
    @PreAuthorize("hasAuthority('USER_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody User user, @Param("roleIds") Integer[] roleIds) {
        try {
            return userService.add(user, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
    }

    /**
     * 根据用户id查询所有角色
     *
     * @param id
     * @return
     */
    @RequestMapping("/findByIdWithAllRole")
    public Result findByIdWithAllRole(Integer id) {
        try {
            Map map = userService.findByIdWithAllRole(id);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }

    /**
     * 编辑用户
     *
     * @param user
     * @param roleIds
     * @return
     */
    @PreAuthorize("hasAuthority('USER_EDIT')")
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user, Integer[] roleIds) {
        try {
            userService.edit(user, roleIds);
            return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('USER_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            return userService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }

    /**
     * 更改用户登录密码
     *
     * @param map
     * @return
     */
    @RequestMapping("/changePassword")
    public Result modifyPassword(@RequestBody Map map) {
        try {
            return userService.modifyPassword(map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.CHANEG_PASSWORD_USER_FAIL);
        }
    }

    @RequestMapping("/showUsername")
    public Result showUsername(Integer id) {
        try {
            User user = userService.showUsername(id);
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }
}
